package df.bean.process;

/**
 *
 * @author T.
 */
import df.bean.db.conn.DBConnection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ProcessJoinBill {

    private String startDate = "";
    private String toDate = "";
    private String hospital_code = "";
    //private String[] invoiceNo = null;

    private String getStartDate() {
        return startDate;
    }

    private String getToDate() {
        return toDate;
    }

    private void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    private void setToDate(String toDate) {
        this.toDate = toDate;
    }
    
    public void setHospitalCode(String hp){
    	this.hospital_code = hp;
    }
    
    private String getHospitalCode(){
    	return this.hospital_code;
    }

    private ResultSet getLineNO(){
        String sql = "SELECT " +
                "LINE_NO, COUNT(LINE_NO) " +
                "FROM INT_HIS_BILL " +
                "WHERE " +
                "AMOUNT_BEF_DISCOUNT > 0 " +
                "AND HOSPITAL_CODE='"+this.getHospitalCode()+"' " +
                "AND BILL_DATE BETWEEN '"+this.getStartDate()+"' AND '"+this.getToDate()+"' " +
                "GROUP BY LINE_NO HAVING COUNT(LINE_NO) > 1;";
        DBConnection con = new DBConnection();
        con.connectToLocal();
        ResultSet rs = null;
        try{
            rs = con.executeQuery(sql);
        }catch(Exception err){
            System.out.println(err.getMessage());
        }
        return rs;
    }
    private void getBillByLineNO(String idRef){
        String sql = "SELECT BILL_NO, LINE_NO, AMOUNT_BEF_DISCOUNT, TRANSACTION_TYPE, AMOUNT_OF_DISCOUNT " +
                " FROM INT_HIS_BILL WHERE LINE_NO = '"+ idRef +"' AND HOSPITAL_CODE = '"+this.getHospitalCode()+"'";
        Double temp_sum = 0.0;
        String temp_line = "";
        String temp_bill = "";
        String sql_insert = "";
        Double temp_discount = 0.0;

        DBConnection con = new DBConnection();
        con.connectToLocal();
        ResultSet rs = con.executeQuery(sql);

        //DBConn c1 = new DBConn();
        try{

            int iCount =0;
            while(rs.next()){
                if("INV".equalsIgnoreCase(rs.getString("TRANSACTION_TYPE")) || iCount==0){
                    temp_line = rs.getString("LINE_NO");
                    temp_bill = rs.getString("BILL_NO");
                    iCount++;
                }
                temp_discount = temp_discount + Double.parseDouble(rs.getString("AMOUNT_OF_DISCOUNT"));
                temp_sum = temp_sum + Double.parseDouble(rs.getString("AMOUNT_BEF_DISCOUNT"));                
            }
            sql_insert += "update INT_HIS_BILL set AMOUNT_BEF_DISCOUNT='0.0', AMOUNT_OF_DISCOUNT='0.0'  where LINE_NO='"+ temp_line +"' AND HOSPITAL_CODE = '"+this.getHospitalCode()+"'";
            sql_insert += "update INT_HIS_BILL set AMOUNT_BEF_DISCOUNT='"+ temp_sum +"', AMOUNT_OF_DISCOUNT ='"+temp_discount+"' where LINE_NO='"+ temp_line +"' AND BILL_NO='"+ temp_bill +"' AND HOSPITAL_CODE = '"+this.getHospitalCode()+"'";
            System.out.println("2 " + sql_insert);
            con.executeUpdate(sql_insert);
        }catch(Exception err){
            System.out.println("Join Bill Error : "+err);
        }finally{
            try{
                //con.Close();
            }catch(Exception err){
                //
            }
        }
        //ResultSet rs =

    }
    public boolean ProcessJoinBill(String startDate, String toDate){
        this.setStartDate(startDate);
        this.setToDate(toDate);
        boolean status = true;
        ResultSet rs = this.getLineNO();
        int count = 0;
        try{
            while(rs.next()){
                System.out.println(rs.getString("LINE_NO"));
                this.getBillByLineNO(rs.getString("LINE_NO"));
                count++;
            }
            //System.out.println("Total Record : " + count);
            
        }catch(Exception err){
            System.out.println(err.getMessage());
            status = false;
        }finally{
            try {
                rs.close();
            } catch (SQLException ex) {
            }
        }
        return status;
    }

    public static void main(String[] arg){
        ProcessJoinBill j = new ProcessJoinBill();
        j.ProcessJoinBill("20081201", "20090130");
    }

    
}
